<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic%7CRoboto+Slab:300,300italic,400,400italic,700,700italic%7CNoto+Serif+SC:300,300italic,400,400italic,700,700italic%7CAmita:300,300italic,400,400italic,700,700italic%7CMontserrat:300,300italic,400,400italic,700,700italic%7CPT+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wang-chen1119.gitee.io","root":"/myblog/","images":"/myblog/images","scheme":"Pisces","darkmode":false,"version":"8.13.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/myblog/js/config.js"></script>

    <meta name="description" content="进程通信与死锁进程通信 什么是进程通信进程通信是指进程之间的信息交换，其所交换的信息量少则是一个状态或数值，多则是成千上万个字节">
<meta property="og:type" content="blog">
<meta property="og:title" content="操作系统学习笔记(四):进程通信与死锁">
<meta property="og:url" content="http://wang-chen1119.gitee.io/myblog/2022/10/28/%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E4%B8%8E%E6%AD%BB%E9%94%81/index.html">
<meta property="og:site_name" content="Yorn&#39;s Notes">
<meta property="og:description" content="进程通信与死锁进程通信 什么是进程通信进程通信是指进程之间的信息交换，其所交换的信息量少则是一个状态或数值，多则是成千上万个字节">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://wang-chen1119.gitee.io/myblog/%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E4%B8%8E%E6%AD%BB%E9%94%81.assets/image-20211230160350357.png">
<meta property="article:published_time" content="2022-10-28T05:57:30.710Z">
<meta property="article:modified_time" content="2022-10-28T06:21:23.742Z">
<meta property="article:author" content="yorn">
<meta property="article:tag" content="操作系统">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://wang-chen1119.gitee.io/myblog/%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E4%B8%8E%E6%AD%BB%E9%94%81.assets/image-20211230160350357.png">


<link rel="canonical" href="http://wang-chen1119.gitee.io/myblog/2022/10/28/%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E4%B8%8E%E6%AD%BB%E9%94%81/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://wang-chen1119.gitee.io/myblog/2022/10/28/%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E4%B8%8E%E6%AD%BB%E9%94%81/","path":"2022/10/28/进程通信与死锁/","title":"操作系统学习笔记(四):进程通信与死锁"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>操作系统学习笔记(四):进程通信与死锁 | Yorn's Notes</title>
  






  <noscript>
    <link rel="stylesheet" href="/myblog/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Yorn's Notes</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-首页"><a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-关于"><a href="/myblog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-标签"><a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-分类"><a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E4%B8%8E%E6%AD%BB%E9%94%81"><span class="nav-number">1.</span> <span class="nav-text">进程通信与死锁</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1"><span class="nav-number">1.1.</span> <span class="nav-text">进程通信</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1"><span class="nav-number">1.1.1.</span> <span class="nav-text">什么是进程通信</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%86%E7%B1%BB"><span class="nav-number">1.1.2.</span> <span class="nav-text">分类</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BD%8E%E7%BA%A7%E9%80%9A%E4%BF%A1"><span class="nav-number">1.1.2.1.</span> <span class="nav-text">低级通信</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%AB%98%E7%BA%A7%E9%80%9A%E4%BF%A1"><span class="nav-number">1.1.2.2.</span> <span class="nav-text">高级通信</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E9%AB%98%E7%BA%A7%E9%80%9A%E4%BF%A1%E5%8F%88%E5%8F%AF%E4%BB%A5%E5%88%86%E6%88%90%E4%B8%89%E5%A4%A7%E7%B1%BB"><span class="nav-number">1.1.2.2.1.</span> <span class="nav-text">高级通信又可以分成三大类</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#1-%E5%85%B1%E4%BA%AB%E5%AD%98%E5%82%A8%E5%99%A8%E7%B3%BB%E7%BB%9F"><span class="nav-number">1.1.2.2.1.1.</span> <span class="nav-text">1. 共享存储器系统</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#2-%E6%B6%88%E6%81%AF%E4%BC%A0%E9%80%92%E7%B3%BB%E7%BB%9F"><span class="nav-number">1.1.2.2.1.2.</span> <span class="nav-text">2. 消息传递系统</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#3-%E7%AE%A1%E9%81%93%E9%80%9A%E4%BF%A1"><span class="nav-number">1.1.2.2.1.3.</span> <span class="nav-text">3.管道通信</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AD%BB%E9%94%81"><span class="nav-number">1.2.</span> <span class="nav-text">死锁</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%AD%BB%E9%94%81%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="nav-number">1.2.1.</span> <span class="nav-text">死锁的定义</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%A7%A3%E5%86%B3%E6%AD%BB%E9%94%81%E7%9A%84%E5%9B%9B%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="nav-number">1.2.1.1.</span> <span class="nav-text">解决死锁的四种方式</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E9%A2%84%E9%98%B2%E6%AD%BB%E9%94%81"><span class="nav-number">1.2.1.1.1.</span> <span class="nav-text">预防死锁</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E9%81%BF%E5%85%8D%E6%AD%BB%E9%94%81"><span class="nav-number">1.2.1.1.2.</span> <span class="nav-text">避免死锁</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%A3%80%E6%B5%8B%E6%AD%BB%E9%94%81"><span class="nav-number">1.2.1.1.3.</span> <span class="nav-text">检测死锁</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E8%A7%A3%E9%99%A4%E6%AD%BB%E9%94%81"><span class="nav-number">1.2.1.1.4.</span> <span class="nav-text">解除死锁</span></a></li></ol></li></ol></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="yorn"
      src="/myblog/uploads/head.jpg">
  <p class="site-author-name" itemprop="name">yorn</p>
  <div class="site-description" itemprop="description">足够专注才能成功</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/myblog/archives/">
          <span class="site-state-item-count">53</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/myblog/categories/">
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/myblog/tags/">
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wang-chen1119" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wang-chen1119" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:w1905989051@gmail.com" title="E-Mail → mailto:w1905989051@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll site-overview-item animated">
    <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://blog.csdn.net/qq_51088445" title="https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_51088445" rel="noopener" target="_blank">CSDN</a>
        </li>
    </ul>
  </div>

        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/wang-chen1119" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://wang-chen1119.gitee.io/myblog/2022/10/28/%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E4%B8%8E%E6%AD%BB%E9%94%81/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/myblog/uploads/head.jpg">
      <meta itemprop="name" content="yorn">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Yorn's Notes">
      <meta itemprop="description" content="足够专注才能成功">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="操作系统学习笔记(四):进程通信与死锁 | Yorn's Notes">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          操作系统学习笔记(四):进程通信与死锁
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2022-10-28 13:57:30 / 修改时间：14:21:23" itemprop="dateCreated datePublished" datetime="2022-10-28T13:57:30+08:00">2022-10-28</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/myblog/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">操作系统</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>1.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>2 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="进程通信与死锁"><a href="#进程通信与死锁" class="headerlink" title="进程通信与死锁"></a>进程通信与死锁</h1><h2 id="进程通信"><a href="#进程通信" class="headerlink" title="进程通信"></a>进程通信</h2><hr>
<h3 id="什么是进程通信"><a href="#什么是进程通信" class="headerlink" title="什么是进程通信"></a>什么是进程通信</h3><p>进程通信是指进程之间的信息交换，其所交换的信息量少则是一个状态或数值，多则是成千上万个字节</p>
<h3 id="分类"><a href="#分类" class="headerlink" title="分类"></a>分类</h3><p>进程通信分为低级通信和高级通信两种</p>
<h4 id="低级通信"><a href="#低级通信" class="headerlink" title="低级通信"></a>低级通信</h4><p>进程的互斥和同步可归结为低级通信</p>
<p>在进程互斥中，进程通过修改信号量向其他进程表明理解资源是否可用</p>
<h4 id="高级通信"><a href="#高级通信" class="headerlink" title="高级通信"></a>高级通信</h4><p>用户可以直接利用操作系统所提供的一组通信命令高效地转送大量数据</p>
<p>高级进程通信方式的通信过程对用户是透明的</p>
<h5 id="高级通信又可以分成三大类"><a href="#高级通信又可以分成三大类" class="headerlink" title="高级通信又可以分成三大类"></a>高级通信又可以分成三大类</h5><h6 id="1-共享存储器系统"><a href="#1-共享存储器系统" class="headerlink" title="1. 共享存储器系统"></a>1. 共享存储器系统</h6><p>互相通信的进程共享某些数据结构或共享存储区，所以分成两类</p>
<ol>
<li>基于共享数据结构的通信方式</li>
<li>基于共享存储区的通信方式</li>
</ol>
<h6 id="2-消息传递系统"><a href="#2-消息传递系统" class="headerlink" title="2. 消息传递系统"></a>2. 消息传递系统</h6><p>进程间的数据交换以消息为单位。程序员直接利用系统提供的一组通信命令来实现通信，也可以分成两种</p>
<ol>
<li>直接通信方式</li>
<li>间接通信方式:发送进程将消息发送到信箱中，接收进程在信箱中取得信息</li>
</ol>
<h6 id="3-管道通信"><a href="#3-管道通信" class="headerlink" title="3.管道通信"></a>3.管道通信</h6><p>管道：用于连接一个读进程和一个写进程已实现它们之间通信的共享文件 —–&gt;pipe文件</p>
<p>管道通信机制必须提供三方面的协调能力：</p>
<ol>
<li>互斥</li>
<li>同步</li>
<li>判断对方是否存在</li>
</ol>
<h2 id="死锁"><a href="#死锁" class="headerlink" title="死锁"></a>死锁</h2><h3 id="死锁的定义"><a href="#死锁的定义" class="headerlink" title="死锁的定义"></a>死锁的定义</h3><p>在多道程序系统中，一组进程中的每一个进程均无期限地等待被改组进程中的另一个进程占有且永远不会释放的资源。</p>
<p><strong>产生死锁的原因</strong></p>
<ol>
<li>竞争资源</li>
<li>进程推进顺序不当</li>
</ol>
<p><strong>产生死锁的必要条件</strong></p>
<ol>
<li>互斥条件   ： 一个资源在一段时间内只能被一个进程占有</li>
<li>请求和保持条件 ： 进程因为请求资源阻塞是，对已经获得的资源保持不放</li>
<li>不剥夺条件：进程获得的资源在使用完之前不能被剥夺</li>
</ol>
<h4 id="解决死锁的四种方式"><a href="#解决死锁的四种方式" class="headerlink" title="解决死锁的四种方式"></a>解决死锁的四种方式</h4><h5 id="预防死锁"><a href="#预防死锁" class="headerlink" title="预防死锁"></a>预防死锁</h5><ol>
<li>抛弃请求和保持条件</li>
</ol>
<p>  即进程一次申请所有需要的资源，进程在运行过程中不会再提出任何资源请求</p>
<p>优点：易于实现，安全</p>
<p>缺点：资源浪费严重，进程延迟进行</p>
<ol start="2">
<li>抛弃不剥夺条件</li>
</ol>
<p>进程已经占有的资源在运行过程中可能被暂时释放</p>
<ol start="3">
<li>抛弃环路等待条件</li>
</ol>
<h5 id="避免死锁"><a href="#避免死锁" class="headerlink" title="避免死锁"></a>避免死锁</h5><p><strong>安全状态</strong> </p>
<blockquote>
<p>系统能够按某种进程顺序，如&lt;P1,P2,P3…..&gt;（安全序列），来为某个进程分配所需资源，直到最大需求，使每个进程都可以顺利完成。</p>
<p>系统存在这样一个安全序列则成为系统处于安全状态，否则处于不安全状态</p>
</blockquote>
<blockquote>
<p>避免死锁的本质就是让系统不进入不安全状态</p>
</blockquote>
<p><strong>避免死锁的银行家算法</strong></p>
<ul>
<li>可利用资源向量Available ： 初始值为系统中所配置的该类全部可用资源的数目</li>
<li>最大需求矩阵Max ：代表某进程需要某资源的最大数目</li>
<li>分配矩阵Allocation ： 表示进程已分得的资源的数目</li>
<li>需求矩阵Need ：表示进程还需要资源的数目</li>
<li>工作向量Work : 表示系统可提供给进程继续运行所需的各资源数目</li>
<li>Finish ：表示系统是否有足够的资源分配给进程使之运行完成</li>
</ul>
<p>以某时刻的系统状态表为例</p>
<table>
<thead>
<tr>
<th align="center">进程</th>
<th align="center"></th>
<th align="center">Max</th>
<th align="center"></th>
<th align="center"></th>
<th align="center">Allocation</th>
<th align="center"></th>
<th align="center"></th>
<th align="center">Need</th>
<th align="center"></th>
<th align="center"></th>
<th align="center">Available</th>
<th align="center"></th>
</tr>
</thead>
<tbody><tr>
<td align="center"></td>
<td align="center">R1</td>
<td align="center">R2</td>
<td align="center">R3</td>
<td align="center">R1</td>
<td align="center">R2</td>
<td align="center">R3</td>
<td align="center">R1</td>
<td align="center">R2</td>
<td align="center">R3</td>
<td align="center">R1</td>
<td align="center">R2</td>
<td align="center">R3</td>
</tr>
<tr>
<td align="center">P1</td>
<td align="center">7</td>
<td align="center">5</td>
<td align="center">3</td>
<td align="center">0</td>
<td align="center">1</td>
<td align="center">0</td>
<td align="center">7</td>
<td align="center">4</td>
<td align="center">3</td>
<td align="center">3</td>
<td align="center">3</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">P2</td>
<td align="center">3</td>
<td align="center">2</td>
<td align="center">2</td>
<td align="center">2</td>
<td align="center">0</td>
<td align="center">0</td>
<td align="center">1</td>
<td align="center">2</td>
<td align="center">2</td>
<td align="center"></td>
<td align="center"></td>
<td align="center"></td>
</tr>
<tr>
<td align="center">P3</td>
<td align="center">9</td>
<td align="center">0</td>
<td align="center">2</td>
<td align="center">3</td>
<td align="center">0</td>
<td align="center">2</td>
<td align="center">6</td>
<td align="center">0</td>
<td align="center">0</td>
<td align="center"></td>
<td align="center"></td>
<td align="center"></td>
</tr>
<tr>
<td align="center">P4</td>
<td align="center">2</td>
<td align="center">2</td>
<td align="center">2</td>
<td align="center">2</td>
<td align="center">1</td>
<td align="center">1</td>
<td align="center">0</td>
<td align="center">1</td>
<td align="center">1</td>
<td align="center"></td>
<td align="center"></td>
<td align="center"></td>
</tr>
<tr>
<td align="center">P5</td>
<td align="center">4</td>
<td align="center">3</td>
<td align="center">3</td>
<td align="center">0</td>
<td align="center">0</td>
<td align="center">2</td>
<td align="center">4</td>
<td align="center">3</td>
<td align="center">1</td>
<td align="center"></td>
<td align="center"></td>
<td align="center"></td>
</tr>
</tbody></table>
<p>以进程P1为例我们可以知道P1进程需要R1 R2 R3 的最大数目为 7 5 3 ，已经拥有的数目为 0 1 0 需要 7 4 3才能够完成任务，目前可用资源数为 3 3 2</p>
<p>具体分析过程：刚开始work &#x3D; available为 3 3 2，P2.need &lt; work所以可以支持P2完成任务，P2完成任务，释放资源数为 2 0 0 此时work变为 5 3 2   —-&gt; Finish[2] &#x3D; 1</p>
<p>然后P4.need &lt; work ,可以支持P4完成工作，P4完成释放资源 2 1 1，work变为 7 4 3   —–&gt; Finish[4] &#x3D; 1</p>
<p>P5.need &lt; work ，P5 完成释放资源 0 0 2，work变为 7 4 5   —–&gt; Finish[5] &#x3D; 1</p>
<p>P3.need &lt;work, P3完成释放资源 3 0 2 ，work 变为 10 4 7   ——&gt; Finish[3] &#x3D;1</p>
<p>P1.need &lt; work,P1完成释放资源 0 1 0，work变为 10 5 7 ——-&gt; Finish[1] &#x3D; 1</p>
<p>得到一条安全序列 2 4 5 3 1 所以该系统处于安全状态</p>
<h5 id="检测死锁"><a href="#检测死锁" class="headerlink" title="检测死锁"></a>检测死锁</h5><ol>
<li>资源分配图</li>
</ol>
<p>以下图为例，进程P1分配到了两个R1，正在请求一个R2 ，线程P2分到了一个R1,一个R2，正在请求R1</p>
<p><img src="/myblog/%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E4%B8%8E%E6%AD%BB%E9%94%81.assets/image-20211230160350357.png" alt="image-20211230160350357"></p>
<ol start="2">
<li><p>死锁定理</p>
<p> 当且仅当S状态资源分配图是不可完全简化的</p>
</li>
</ol>
<p>死锁的检测算法可以采用银行家算法中的安全检测算法来完成</p>
<h5 id="解除死锁"><a href="#解除死锁" class="headerlink" title="解除死锁"></a>解除死锁</h5><p>解除死锁的两种方法</p>
<ol>
<li>剥夺资源 ：从其他进程中剥夺足够的资源给死锁进程</li>
<li>撤销进程 ：将全部死锁进程清除掉</li>
</ol>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>yorn
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://wang-chen1119.gitee.io/myblog/2022/10/28/%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E4%B8%8E%E6%AD%BB%E9%94%81/" title="操作系统学习笔记(四):进程通信与死锁">http://wang-chen1119.gitee.io/myblog/2022/10/28/进程通信与死锁/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/myblog/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag"><i class="fa fa-tag"></i> 操作系统</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/myblog/2022/10/28/%E8%BF%9B%E7%A8%8B%E8%B0%83%E5%BA%A6%E4%B8%8E%E7%BA%BF%E7%A8%8B/" rel="prev" title="操作系统学习笔记(二):进程调度与线程">
                  <i class="fa fa-chevron-left"></i> 操作系统学习笔记(二):进程调度与线程
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/myblog/2022/10/28/%E8%BF%9B%E7%A8%8B%E4%B8%8E%E7%A8%8B%E5%BA%8F/" rel="next" title="进程与程序">
                  进程与程序 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">yorn</span>
</div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/myblog/js/comments.js"></script><script src="/myblog/js/utils.js"></script><script src="/myblog/js/motion.js"></script><script src="/myblog/js/next-boot.js"></script><script src="/myblog/js/bookmark.js"></script>

  





  





<script src="/myblog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
